import { defineStore } from 'pinia'

export const userStore = defineStore({
  id: 'user',
  state: () => {
    const token = localStorage.getItem('token') || ''
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
    return {
      token: token,
      userInfo: userInfo
    }
  },
  actions: {
    setUserInfo(data: { token: string; userInfo: {} }) {
      this.token = data.token
      this.userInfo = data.userInfo
      localStorage.setItem('token', this.token)
      localStorage.setItem('userInfo', JSON.stringify(this.userInfo))
    }
  }
})
